package com.budwk.app.sqfwV2.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfwV2.service.roomSafety.RoomSafetyAssistantService;
import com.budwk.app.sqfwV2.service.roomSafety.ThirdPartyService;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @author ywm
 * @since 2024/6/15
 */
@IocBean
@At("platform/assistance/roomSafetyAssistant")
@ApiDefinition(tag = "房安助手")
@Slf4j
public class RoomSafetyAssistantController {

    @Inject
    private RoomSafetyAssistantService roomSafetyAssistantService;

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取房屋列表")
    @SaCheckLogin
    public Result<?> building(@Param("regionCityName") String regionCityName, @Param("regionCommunityName") String regionCommunityName,
                                  @Param("regionVillageName") String regionVillageName, @Param("houseName") String houseName,
                                  @Param("pageNo") int pageNo, @Param("pageSize") int pageSize) {
        Object o = ThirdPartyService.building(regionCityName, regionCommunityName, regionVillageName, houseName, pageNo, pageSize);
        Pagination pagination = new Pagination();
        JSONObject jsonObject = JSON.parseObject(o.toString());
        pagination.setTotalCount(jsonObject.getInteger("count"));
        pagination.setList(jsonObject.getJSONArray("list"));
        return Result.data(pagination);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取字典")
    @SaCheckLogin
    public Result<?> findDictSelect(@Param("code") String code) {
        return Result.data(ThirdPartyService.findDictSelect(code));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取行政区")
    @SaCheckLogin
    public Result<?> getDistrict(@Param("name") String name) {
        return Result.data(ThirdPartyService.getDistrict(name));
    }

    @At
    @Ok("json")
    @POST
    @AdaptBy(type = UploadAdaptor.class)
    @ApiOperation(name = "图片上传")
    @SaCheckLogin
    public Result<?> upload(@Param("file") TempFile file) {
        return Result.data(ThirdPartyService.upload(file));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取图片")
    @SaCheckLogin
    public Result<?> v_image(@Param("path") String path, @Param("gzip") String gzip) {
        return Result.data(ThirdPartyService.v_image(path, gzip));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加房屋")
    @SaCheckLogin
    public Result<?> addBuilding(@Param("..") JSONObject jsonObject) {
        return Result.data(ThirdPartyService.addBuilding(jsonObject));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改房屋")
    @SaCheckLogin
    public Result<?> editBuilding(@Param("..") JSONObject jsonObject) {
        return Result.data(ThirdPartyService.editBuilding(jsonObject));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取房屋详情")
    @SaCheckLogin
    public Result<?> queryDetailById(@Param("id") String id) {
        return Result.data(ThirdPartyService.queryDetailById(id));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取隐患整改列表（待整改，已整改）")
    @SaCheckLogin
    public Result<?> rectificationList(@Param("regionCityId") String regionCityId, @Param("regionCommunityId") String regionCommunityId,
                                       @Param("status") String status) {
        return Result.data(ThirdPartyService.rectificationList(regionCityId, regionCommunityId, status));
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取整改详情")
    @SaCheckLogin
    public Result<?> queryRectificationById(@Param("id") String id) {
        return Result.data(ThirdPartyService.queryRectificationById(id));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "添加隐患")
    @SaCheckLogin
    public Result<?> check(@Param("..") JSONObject jsonObject) {
        roomSafetyAssistantService.add(jsonObject, false);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "隐患整改")
    @SaCheckLogin
    public Result<?> rectification(@Param("..") JSONObject jsonObject) {
        roomSafetyAssistantService.update(jsonObject, false);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "第三方添加隐患")
    public Result<?> addHidden(@Param("..") JSONObject jsonObject, HttpServletRequest request) {
        ThirdPartyService.auth(request);

        roomSafetyAssistantService.add(jsonObject, true);

        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "第三方隐患整改")
    public Result<?> rectificationHidden(@Param("..") JSONObject jsonObject, HttpServletRequest request) {
        ThirdPartyService.auth(request);

        roomSafetyAssistantService.update(jsonObject, true);

        return Result.success();
    }
}
